<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Categories_lib {

    var $settings;
    var $option_ar = array();
    var $value_ar = array();
    var $id_name_ar = array();
    var $option_store = array();
    var $name_prefix = "----";
    var $rows = array();

    function Categories_lib() {
        $this->CI = & get_instance();
        $this->CI->load->model('category');
    }

    function build_tree($root_id) {
        $filter = array('id_parent' => $root_id);
        $data = $this->CI->category->find_by_id($root_id);
        $list_item = $this->CI->category->get_all_category($filter);
        $t = array();
        foreach ($list_item as $item) {
            $t[] = $this->build_tree($item["id"]);
        }
        if (count($t) > 0) {
            $data["children"] = $t;
        }
        return $data;
    }

    function get_ids_by_parent($root_id) {
        $data = $this->CI->category->get_child($root_id);
        $t = array();
        foreach ($data as $item) {
            $t[] = $item['id'];
            $t = array_merge($t, $this->get_ids_by_parent($item['id']));
        }
        return $t;
    }

    function build_tree_option($root_id, $name, $deep) {
        $filter = array('id_parent' => $root_id);
        if ($deep > 0) {
            $this->option_ar[] = repeater($this->name_prefix, $deep - 1) . $name;
            $this->value_ar[] = $root_id;
            $this->id_name_ar[$root_id] = $name;
            $this->option_store[] = array('id' => $root_id, "name" => repeater($this->name_prefix, $deep - 1) . $name);

            //$row['id'] = $root_id;
            $row['display'] = repeater($this->name_prefix, $deep - 1) . $name;
            $row['value'] = $root_id;
            $row['deep'] = $deep;
            $this->rows[] = $row;
        }
        $data = $this->CI->category->find_by_id($root_id);
        $list_item = $this->CI->category->get_all_category($filter);
        foreach ($list_item as $item) {
            $this->build_tree_option($item["id"], $item["name"], $deep + 1);
        }
        //$this->name_prefix .=$this->name_prefix;
    }

    static function buildHTML($option, $current, $attr='name="category_select"') {
        $out = '<select ' . $attr . '>';
        $out .= '<option value="0">----</option>';
        foreach ($option as $item) {
            $out .= '<option value="' . $item['value'] . '"';
            if ($current == $item['value'])
                $out .= ' selected="selected"';
            $out .= '>' . $item['display'] . '</option>';
        }
        $out .= '</select>';
        return $out;
    }

    function json_for_tree($root_id) {
        $t = $this->build_tree($root_id);
        $data['categories_list'] = $t;
        $this->CI->load->view(backend_view_path() . 'categories/categories_json', $data);
    }

    function build_gicms_category_tree() {
        $data = array();
        $cats = $this->CI->category->get_categories();
        //print_r($cats);die;
        foreach ($cats as $cat) {
            $item = array();
            $item['id'] = $cat['id'];
            $item['name'] = $cat['name'];
            $children = $this->CI->category->get_child($cat['id']);
            if (count($children) > 0) {
                $c = array();
                foreach ($children as $child) {
                    $c[] = array('_reference' => $child['id']);
                }
                $item['numberOfItems'] = count($children);
                $item['children'] = $c;
            }
            $data[] = $item;
        }
        return '{identifier: "id",
    label: "name",
    items:' . json_encode($data) . '}';
    }

    function build_shop_category_tree($root_id) {
        $data = array();
        $cats = $this->CI->category->get_shop_categories($root_id);
        //print_r($cats);die;
        foreach ($cats as $cat) {
            $item = array();
            $item['id'] = $cat['id'];
            $item['name'] = $cat['name'];
            $children = $this->CI->category->get_shop_child($cat['id']);
            if (count($children) > 0) {
                $c = array();
                foreach ($children as $child) {
                    $c[] = array('_reference' => $child['id']);
                }
                $item['numberOfItems'] = count($children);
                $item['children'] = $c;
            }
            $data[] = $item;
        }
        return '{identifier: "id",
    label: "name",
    items:' . json_encode($data) . '}';
    }

    function delete_child_categories($roor_id) {
        $children = $this->CI->category->get_child($roor_id);
        if (count($children) > 0) {
            foreach ($children as $child) {
                $this->delete_child_categories($child['id']);
            }
        } else {
            $this->CI->category->delete_category($roor_id);
        }
    }

    function delete_category($id_category) {
        $this->CI->category->delete_category($id_category);
    }

}

// EOF
?>